﻿#include"SupplyBank.h"
SupplyBank::SupplyBank()
{
	init();
}
SupplyBank::SupplyBank(int max)
	:m_max(max)
{
	init();
}

void SupplyBank::addSupply(const QPoint& point, const Supply::Type type)
{
	addSupply(point.x(), point.y(), type);
}

void SupplyBank::addSupply(const int x, const int y, const Supply::Type type)
{
	if (m_SupplyBank.count() >= m_max)
		return;
	Supply supply(x,y,type,QVector2D(0,1), m_Property[type].Steplen);
	supply.addFrame(m_Property[type].ModelName);
	supply.setWindow(m_Window);
	/*if (m_Plane != nullptr)
		supply.bindPlane(m_Plane);*/
	m_SupplyBank.append(supply);
}

//void SupplyBank::bindPlane(Sprite* const plane)
//{
//	m_Plane = plane;
//}

void SupplyBank::setTimerSupply(const int MinTime, const int MaxTime, const Supply::Type type)
{
	if (MinTime > MaxTime)
	{
		OutInfo;
		qDebug() << "最小值大于最大值";
	}
	m_Property[type].MaxTime = MaxTime;
	m_Property[type].MinTime = MinTime;
	m_Property[type].setUpTime();
}

void SupplyBank::setSteplenSupply(const int Steplen, const Supply::Type type)
{
	m_Property[type].Steplen = Steplen;
}

void SupplyBank::setSupplyType(const int type)
{
	m_SupplyType = type;
}

void SupplyBank::setWindow(const QRect& rect)
{
	m_Window = rect;
	for (auto& v : m_Property)
	{
		v.maxRight=m_Window.width()- getFrame(v.ModelName)->begin()->width();
	}
}

void SupplyBank::startAuto()
{
	/*qDebug() << m_Property.count();*/
	for (auto it = m_Property.begin(); it != m_Property.end(); it++)
	{
		if (it.key() & m_SupplyType)
			it->Timer.start();
		else
			it->Timer.stop();
	}
	//定时器自动放飞机函数设置
	AutoSupply();
}

void SupplyBank::closeAuto()
{
	for (auto& v : m_Property)
	{
		v.Timer.stop();
	}
}

QVector<Supply>& SupplyBank::getSupplyBank()
{
	return m_SupplyBank;
}

void SupplyBank::init()
{
	m_Property[Supply::Type::bullet] = SupplyProperty(15000,30000,100,"子弹补给");
	m_Property[Supply::Type::bomb] = SupplyProperty(20000, 30000, 150, "炸弹补给");
}

void SupplyBank::update()
{
	for (auto it = m_SupplyBank.begin(); it != m_SupplyBank.end();it++)
	{
		it->update();
		it->AutoMove();
		if (!it->isActive())
		{
			it = m_SupplyBank.erase(it);//释放不是活动的补给包
			if (it == m_SupplyBank.end())
				break;
		}
	}
}

void SupplyBank::draw(QPainter* painter)
{
	for (auto &v: m_SupplyBank)
	{
		v.draw(painter);
	}
}

void SupplyBank::setPause(bool flag)
{
	Background::setPause(flag);
	for (auto& v : m_SupplyBank)
	{
		v.setPause(flag);
	}
	if (flag)
	{
		closeAuto();
	}
	else
	{
		startAuto();
	}
}

void SupplyBank::AutoSupply()
{
	if (m_Window.isNull())
	{
		OutInfo;
		qWarning() << "请在外部调用setCollider,缺少重要数据(边界)，程序无法继续运行,无法放出敌机";
		return;
	}
	/*if (m_Plane == nullptr)
	{
		qWarning() << "请在外部调用bindWeaponBank设置武器库,不然无法完成补给";
	}*/
	for (auto it = m_Property.begin(); flag&& it != m_Property.end(); it++)
	{
		it->Timer.callOnTimeout([=]() {
			int x = random(0, it->maxRight);
			int y = -getFrame(it->ModelName)->begin()->height() * 0.9;
			//qDebug() << x << "," << y;
			addSupply(x, y, Supply::Type(it.key()));
			it->setUpTime();
			});
	}
	flag = false;
}
